﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Meqs.Entity;
using Meqs.Logic.Interface;

public partial class Score_School_ForManage_ShowClassStatus : System.Web.UI.Page
{
    /// <summary>
    /// 学校开设课程
    /// </summary>
    protected IList<GlobalCauseInfo> schoolglobalCause = new List<GlobalCauseInfo>();
    protected IList<ClsInfo> clsinfo;
    protected IList<ClassResultExamList> clsResultList;
    protected IList<TeachClsCause> teachclsCause;
    protected IList<TeacherRoles> roles;
    protected int Year
    {
        get
        {
            string grade = HttpContext.Current.Request.QueryString["year"];
            int temp = 0;
            if (int.TryParse(grade, out temp))
            {
                return temp;
            }
            return 0;
        }
    }

    protected int Term
    {
        get
        {
            string grade = HttpContext.Current.Request.QueryString["term"];
            int temp = 0;
            if (int.TryParse(grade, out temp))
            {
                return temp;
            }
            return 0;
        }
    }

    protected int ExamType
    {
        get
        {
            string grade = HttpContext.Current.Request.QueryString["examtype"];
            int temp = 0;
            if (int.TryParse(grade, out temp))
            {
                return temp;
            }
            return 0;
        }
    }

    protected int Grade
    {
        get
        {
            string grade = HttpContext.Current.Request.QueryString["grade"];
            int temp = 0;
            if (int.TryParse(grade, out temp))
            {
                return temp;
            }
            return 0;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Hashtable htParam = new Hashtable();
            htParam.Add("IsEffect", 1);
            List<GlobalCauseInfo> globalCause = (List<GlobalCauseInfo>)Container<IGlobalCauseInfoLogic>.Instance.FindAll(htParam);

            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", Common.GetConstSchoolMark());
            List<CauseInfo> SchoolCause = (List<CauseInfo>)Container<ICauseInfoLogic>.Instance.FindAll(ht);
            ht.Clear();
            ht.Add("SchoolMark", Common.GetConstSchoolMark());
            ht.Add("Grade", Convert.ToInt32(Grade));
            IList<CouseGradeInfo> couinfo = Container<ICouseGradeInfoLogic>.Instance.FindAll(ht);
            foreach (GlobalCauseInfo g in globalCause)
            {
                if (CheckIn(SchoolCause, g.RID))
                {
                    bool add = false;
                    foreach (CouseGradeInfo cgi in couinfo)
                    {
                        if (cgi.CourseID == g.RID)
                        {
                            add = true;
                            break;
                        }
                    }
                    if (add)
                    {
                        schoolglobalCause.Add(g);
                    }
                }
            }
           
            ht.Clear();
            ht.Add("SchoolMark",Common.GetConstSchoolMark());
            ht.Add("SchoolYear", Year);
            ht.Add("Term", Term);
            ht.Add("Part", ExamType);
            clsResultList = Container<IClassResultExamListLogic>.Instance.FindAll(ht);
            ht.Clear();
            ht.Add("TeachID", Common.GetUserInfo().UID);
            teachclsCause = Container<ITeachClsCauseLogic>.Instance.FindAll(ht);
            clsinfo = Container<IClassHistoryLogic>.Instance.FindClassesInSchoolYear(Year,Common.GetConstSchoolMark(),Grade);

            ht.Clear();

            ht.Add("SchoolMark", Common.GetConstSchoolMark());
            ht.Add("GreadID", Convert.ToInt32(Grade));
            ht.Add("SchoolYear", Year);
            ht.Add("Term", Term);
            ht.Add("RoleID", 8);
            ht.Add("TeacherID", CommonScore.GetUserID);
            roles = Container<ITeacherRolesLogic>.Instance.FindAll(ht);

            Repeater1.DataSource = clsinfo;
            Repeater1.DataBind();
        }
    }

    private bool CheckIn(List<CauseInfo> SchoolCause, int causeid)
    {
        foreach (CauseInfo c in SchoolCause)
        {
            if (c.RID == causeid)
                return true;
        }
        return false;
    }

    protected string GetHeader()
    {
        string str = "";
        str += "<th style='text-align:left;padding-left:5px;width:20%'>班级</th>";
        if (schoolglobalCause.Count > 0)
        {
            float w = 80 / schoolglobalCause.Count;
            foreach (GlobalCauseInfo g in schoolglobalCause)
            {
                str += "<th style='width:" + w + "%'>";
                str += g.CauseName;
                str += "</th>";
            }
        }
        return str;
    }

    protected string GetBody(int rid, string name)
    {
        string str = "";
        str += "<td style='text-align:left;padding-left:5px;'>" + name + "</td>";
        foreach (GlobalCauseInfo g in schoolglobalCause)
        {
            str += "<td >";
            bool ishave = false;
            foreach (ClassResultExamList cre in clsResultList)
            {
                if (cre.CauseID == g.RID && cre.ClassID == rid)
                {
                    ishave = true;
                    break;
                }
            }
            bool isteach = false;
            foreach (TeachClsCause tcc in teachclsCause)
            {
                if (tcc.CauseID == g.RID && tcc.TeachID == Common.GetUserInfo().UID && tcc.ClsID == rid)
                {
                    isteach = true;
                    break;
                }
            }
            
            foreach(TeacherRoles tr in roles)
            {
                if(g.RID == tr.CauseID)
                {
                    isteach = true;
                    break;
                }
            }


            if (ishave)
            {
                if (isteach)
                {
                    str += "<a href='#' onclick='OnInput(" + 0 + "," + Year + "," + Term + "," + ExamType + "," + rid + "," + g.RID + ","+Grade+")'>已录入</a>";
                }
                else
                {
                    str += "已录入";
                }
            }
            else
            {
                if (isteach)
                {
                    str += "<span style='color:Red;'><a href='#' onclick='OnInput(" + 0 + "," + Year + "," + Term + "," + ExamType + "," + rid + "," + g.RID + ","+Grade+")'>未录入</a></span>";
                }
                else
                {
                    str += "<span style='color:Red;'>未录入</span>";
                }
            }
            str += "</td>";
        }
        return str;
    }
}
